<template>
  <el-dialog title="批量添加" v-model="openBatch" width="600px" append-to-body :close-on-click-modal="false"
             @close="cancelBatch">
    <el-form ref="formBatchRef" :model="formBatch" :rules="rulesBatch" label-width="100px">
      <el-form-item label="是否连号" prop="sns">
        <el-switch active-text="是" active-color="#085" inactive-text="否" inactive-color="#f00"
                   v-model="serial.is_serial"></el-switch>
      </el-form-item>
      <el-form-item label="连号设备号" prop="sns" v-if="serial.is_serial == true">
        <el-row>
          <el-col :span="12">
            <el-input v-model="serial.minSn" placeholder="请输入第一个连号设备号"/>
          </el-col>
          <el-col :span="12">
            <el-input v-model="serial.maxSn" placeholder="请输入最后一个连号设备号"/>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="设备号" prop="sns" v-if="serial.is_serial == false">
        <div v-for="(item, key) in formBatch.sns" :key="key">
          <el-row>
            <el-col :span="20">
              <el-input v-model="item.sn"></el-input>
            </el-col>
            <el-col :span="4">
              <el-button v-if="key >= 0 && key < 25" type="danger" circle icon="Delete" @click="delSn(key)"/>
            </el-col>
          </el-row>
        </div>
        <el-button v-if="formBatch.sns.length < 25" type="primary" circle icon="Plus" @click="addSn"/>
      </el-form-item>
      <el-form-item label="设备状态" prop="devStatus">
        <el-radio-group v-model="formBatch.devStatus">
          <el-radio v-for="dict in dev_status" :key="dict.value" :label="dict.value">{{
              dict.label
            }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="设备版本" prop="version">
        <el-radio-group v-model="formBatch.version">
          <el-radio v-for="dict in dev_version" :key="dict.value" :label="dict.value">{{
              dict.label
            }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="启用/禁用" prop="delFlag">
        <el-switch active-text="正常" active-color="#085" :active-value="0" inactive-text="禁用" inactive-color="#f00"
                   :inactive-value="1" v-model="formBatch.delFlag"></el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitBatch">确 定</el-button>
        <el-button @click="cancelBatch">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup name="DialogBatchAdd">
import {batchAddDev} from "@/api/equipment/dev";

const {proxy} = getCurrentInstance();

const {dev_status, dev_version} = proxy.useDict("dev_status", "dev_version");
const openBatch = ref(false);
const formBatch = ref({
  id: null,
  sns: [
    {
      sn: null,
    },
  ],
  devStatus: "OFF",
  version: "104",
  delFlag: null,
})
const serial = ref({
  minSn: null,
  maxSn: null,
  is_serial: false,
})
const rulesBatch = ref({
  sns: [{required: true, message: "设备号不能为空", trigger: "blur"}],
  devStatus: [
    {required: true, message: "设备状态不能为空", trigger: "blur"},
  ],
  mode: [
    {required: true, message: "设备模式不能为空", trigger: "blur"},
  ],
  version: [
    {required: true, message: "设备版本不能为空", trigger: "blur"},
  ],
  delFlag: [
    {required: true, message: "启用/禁用不能为空", trigger: "blur"},
  ],
})

function submitBatch() {
  let sn_list = [];
  if (serial.is_serial.value == true) {
    if (serial.minSn.value == "") serial.minSn.value == null;
    if (serial.maxSn.value == "") serial.maxSn.value == null;
    if (serial.minSn.value == null || serial.maxSn.value == null) {
      proxy.$modal.notifyError("输入的设备号不能为空，请检查后提交！");
      return;
    }
    for (
      let i = parseInt(serial.minSn.value);
      i <= parseInt(serial.maxSn.value);
      i++
    ) {
      sn_list.push(i);
    }
  } else {
    for (let i = 0; i < formBatch.value.sns.length; i++) {
      for (let j = 0; j < formBatch.value.sns.length; j++) {
        if (
          formBatch.value.sns[i].sn == formBatch.value.sns[j].sn &&
          i != j
        ) {
          that.$modal.notifyError("输入的设备号重复，请检查后提交！");
          return;
        }
      }
      sn_list.push(formBatch.value.sns[i].sn);
    }
  }
  formBatch.value.sns = sn_list;
  if (formBatch.value.sns.length == 0) {
    proxy.$modal.notifyError("输入的设备号不能为空，请检查后提交！");
    return;
  }
  proxy.$refs["formBatchRef"].validate((valid) => {
    if (valid) {
      batchAddDev(formBatch.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        cancelBatch();
        if (reload.value) reload();
      });
    }
  });
}

function open() {
  openBatch.value = true;
}

// 暴露出去
defineExpose({
  open,
})

function cancelBatch() {
  openBatch.value = false;
  resetBatch();
}

function addSn() {
  formBatch.value.sns.push({sn: null});
}

function delSn(index) {
  formBatch.value.sns.splice(index, 1);
  if (formBatch.value.sns.length == 0) {
    addSn();
  }
}

function resetBatch() {
  formBatch.value = {
    id: null,
    sns: [{sn: null}],
    devStatus: "OFF",
    version: "104",
    delFlag: null,
  };
  serial.value = {
    minSn: null,
    maxSn: null,
    is_serial: false,
  };
  proxy.resetForm("formBatchRef");
}
</script>
